package auth

import (
	"MembershipRegist/controllers/ctrl"
	"MembershipRegist/db"
	"MembershipRegist/models"
	"errors"
)

type OrganizationAdminController struct {
	ctrl.Controller
	OrganizationAdmin   *models.OrganizationAdmin
	OrganizationAdminID uint
}

var (
	ErrOrganizationAdminNotLogin = errors.New("organization admin not login")
)

func (o *OrganizationAdminController) Prepare() {
	adminID := o.GetSession("organization_admin_id")
	if adminID == nil {
		o.Abort("403")
		return
	}
	var admin models.OrganizationAdmin
	if value := db.DB.Preload("Organization").
		Where("id = ?", adminID.(uint)).First(&admin); value.Error != nil {
		o.Abort("403")
		return
	}
	o.OrganizationAdmin = &admin
	o.OrganizationAdminID = admin.ID
	return
}

func (o *OrganizationAdminController) AdminInfo() {
	result := make(map[string]interface{})
	result["status"] = 200
	result["message"] = "success"
	result["admin"] = *o.OrganizationAdmin
	o.Data["json"] = result
	o.ServeJSON()
}
